// pages/mine/payDetails/payDetails.js
const app = getApp();
const util = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: "",
    order: []
  },

  // 取消订单
  cancelOrder: function (e) {
    let that = this
    // console.log(e.currentTarget.id)
    wx.showModal({
      title: '取消订单',
      content: '您确认取消该订单吗',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        // console.log(res);
        if (res.confirm) {
          // 发送取消订单请求
          let cancleOrderUrl = app.globalData.baseUrl + "weixin/cancleOrder"
          let data = {
            orderId: that.data.orderId
          }
          util.requestLoading(cancleOrderUrl, data, "GET").then(res => {
            // console.log(res)
            wx.reLaunch({
              url: '../myform',
            })
          })
        }
      }
    })
  },

  // 付款
  toPayDetails: function (e) {
    let that = this
    let productName = that.data.order.order[0].productName
    let orderId = that.data.order.orderId
    let money = that.data.order.shouldPay
    // console.log(money)
    wx.showLoading()
    // 发送支付请求
    let payUrl = app.globalData.baseUrl + "weixin/pay"
    let openid = wx.getStorageSync('openId')
    let data = {
      openid: openid,
      // productName: productName,
      orderId: orderId,
      money: money
    }
    util.requestLoading(payUrl, data, "GET").then(res => {
      // console.log(res)
      if (res.statusCode == 200) {
        wx.hideLoading()
        let openId = wx.getStorageSync("openId")
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: 'MD5',
          paySign: res.data.paySign,
          success(res) {
            // console.log(res)
            // // 发送成功回调请求
            // let payResultUrl = app.globalData.baseUrl + "weixin/payResult"
            // let data = {
            //   orderId: orderId,
            //   money: money,
            //   status: 0
            // }
            // util.requestLoading(payResultUrl, data, "GET").then(res => {
            //   // console.log(res)
            // })
            wx.showModal({
              content: '支付成功',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '../../mine'
                  })
                }
              }
            })
          },
          fail(res) {
            // console.log(res)
            // // 发送失败回调请求
            // let payResultUrl = app.globalData.baseUrl + "weixin/payResult"
            // let data = {
            //   orderId: orderId,
            //   money: money,
            //   status: 1
            // }
            // util.requestLoading(payResultUrl, data, "GET").then(res => {
            //   // console.log(res)
            // })
            wx.reLaunch({
              url: '../../mine',
            })
          }
          
        })
      }
    })
  },
  //修改地址
  changeAddress:function(){
      wx.navigateTo({
        url: "../../address/address?orderId=" + this.data.orderId,
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options.orderId)
    let that = this
    that.setData({
      orderId: options.orderId 
    })
    // 请求页面数据
    let waitPayOrdersDetailsUrl = app.globalData.baseUrl + "weixin/waitPayOrdersDetail"
    let cid = wx.getStorageSync('cid')
    let data = {
      orderId: that.data.orderId
    }
    util.requestLoading(waitPayOrdersDetailsUrl, data, "GET").then(res => {
      console.log(res)
      that.setData({
        order: res.data.order
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})